//郑老板
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int num[2001], tot = 0;   // num[]线性表存树 tot记录最深层数
vector<int> out;          //输出序列
void pre(int i, int deep) //中序遍历 左根右
{
    tot = max(deep, tot);
    if (num[i] == 0) //不存在该节点 退出
        return;
    if (num[i * 2] != 0)
        pre(i * 2, deep + 1); //存在左子树 递归
    out.push_back(num[i]);    //存储 当前层 根元素
    if (num[i * 2 + 1] != 0)  //存在右子树 递归
        pre(i * 2 + 1, deep + 1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    for (int i = 0; i < t; i++)
    {
        int n;
        tot = 0;
        cin >> n;
        for (int j = 1; j <= 2000; j++)
            num[j] = 0;
        for (int j = 1; j <= n; j++)
            cin >> num[j];
        out.clear();
        pre(1, 1);
        vector<int>::iterator iter = out.begin();
        cout << *iter;
        iter++;
        for (; iter != out.end(); iter++)
            cout << " " << *iter;
        cout << '\n';
        /*
        vector<int> tmp;
        for (auto j : out)
            tmp.push_back(j);
        for (int j = 0; j < tmp.size() - 1; j++)
            cout << tmp[j] << " ";
        cout << tmp[tmp.size() - 1] << '\n';
        */
        cout << tot << '\n';
    }
    return 0;
}